package gulimall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import gulimall.constant.ProductConstant;
import gulimall.entity.Attr;
import gulimall.entity.AttrAttrgroupRelation;
import gulimall.entity.AttrGroup;
import gulimall.mapper.AttrAttrgroupRelationMapper;
import gulimall.mapper.AttrGroupMapper;
import gulimall.mapper.AttrMapper;
import gulimall.service.AttrService;
import gulimall.service.IAttrGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import gulimall.utils.PageUtils;
import gulimall.utils.Query;
import gulimall.vo.AttrGroupWithAttrVo;
import gulimall.vo.AttrVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 属性分组 服务实现类
 * </p>
 *
 * @author HLF
 * @since 2022-04-23
 */
@Service
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroup> implements IAttrGroupService {

    @Resource
    private AttrGroupMapper attrGroupMapper;

    @Resource
    private AttrMapper attrMapper;

    @Resource
    private AttrService attrService;

    @Resource
    private AttrAttrgroupRelationMapper relationMapper;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroup> page = this.page(
                new Query<AttrGroup>().getPage(params),
                new QueryWrapper<AttrGroup>()
        );

        return new PageUtils(page);
    }

    /**
     * 根据分类id查询该分类的所有属性分组
     *
     * @param params    分页参数
     * @param catelogId 分类id，如果分类id为0则显示所有属性分组
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        //获取检索条件
        String key = (String) params.get("key");
        /**
         * 多字段模糊匹配
         * catelogId !=0 的查询语句：
         * select * from pms_attr_group from pms_attr_group
         * where catelogId = ?
         * and (attr_group_name like '%key%' or attr_group_id = key or descript like '%key%')
         */
        QueryWrapper<AttrGroup> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(key)) {
            queryWrapper.and(wrapper -> {
                wrapper.lambda().like(AttrGroup::getAttrGroupName, key)
                        .or().eq(AttrGroup::getAttrGroupId, key)
                        .or().like(AttrGroup::getDescript, key);
            });
        }

        if (catelogId != 0) {//当不是查询全部的时候
            queryWrapper.lambda().eq(AttrGroup::getCatelogId, catelogId);
        }

        IPage<AttrGroup> page = this.page(
                new Query<AttrGroup>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    /**
     * 删除属性与分组的关联关系
     *
     * @param relationVoList
     */
    @Override
    public void deleteRelation(List<AttrAttrgroupRelation> relationVoList) {
        attrGroupMapper.deleteBatchRelation(relationVoList);
    }

    /**
     * 获取分类下所有分组&关联属性
     *
     * @param catelogId 分类id
     * @return
     */
    @Override
    public List<AttrGroupWithAttrVo> getGroupWithAttrByCatelogId(Long catelogId) {
        //1.根据分类id查询该分类的所有分组
        List<AttrGroup> list = attrGroupMapper.selectList(new QueryWrapper<AttrGroup>()
                .lambda().eq(AttrGroup::getCatelogId, catelogId)
        );

        //2.查询每个分组下的所有属性并给AttrGroupWithAttrVo的属性分组相关信息和attrs赋值
        List<AttrGroupWithAttrVo> attrGroupWithAttrVos = list.stream().map(attrGroup -> {
            AttrGroupWithAttrVo groupWithAttrVo = new AttrGroupWithAttrVo();
            //2.1给groupWithAttrVo赋值属性分组的相关信息
            BeanUtils.copyProperties(attrGroup, groupWithAttrVo);
            //2.2给groupWithAttrVo的attrs赋值
            //根据分组id查询出所有的属性
            /*List<AttrAttrgroupRelation> relations = relationMapper.selectList(new QueryWrapper<AttrAttrgroupRelation>()
                    .lambda().eq(AttrAttrgroupRelation::getAttrGroupId, attrGroup.getAttrGroupId())
            );
            List<Long> attrIds = relations.stream().map(AttrAttrgroupRelation::getAttrId).collect(Collectors.toList());
            List<Attr> attrs = attrService.listByIds(attrIds);*/
            List<Attr> attrs = attrService.getRelationAttr(attrGroup.getAttrGroupId());

            //将attr对象转换为attrVo对象
            List<AttrVo> attrVos = attrs.stream().map(attr -> {
                AttrVo attrVo = new AttrVo();
                BeanUtils.copyProperties(attr, attrVo);
                attrVo.setAttrGroupId(attrGroup.getAttrGroupId());
                return attrVo;
            }).collect(Collectors.toList());
            //给groupWithAttrVo的attrs赋值
            groupWithAttrVo.setAttrs(attrVos);
            return groupWithAttrVo;
        }).collect(Collectors.toList());

        return attrGroupWithAttrVos;
    }

}
